package com.xjscrm.console.chat.core.impl.conversation;

import cn.hutool.core.util.IdUtil;
import com.uzai.mobile.collect.api.MicaTopics;
import com.uzai.mobile.collect.api.dto.MicaTriggerDto;
import com.uzai.mobile.collect.api.dto.trigger.group.MicaTriggerGroupExitMsg;
import com.uzai.mobile.collect.api.enums.MicaTriggerType;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.frame.recv.RConversationKickMemberMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import com.xjscrm.console.entity.ChatTalkSession;
import com.xjscrm.console.mq.producer.SendMsgProducer;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionService;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionStaffRelService;
import com.xjscrm.console.service.customer.wwDevice.WwDeviceService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/17 09:42
 */
@Component
@Slf4j
public class ChatConversationExitMsgHandler extends ChatBaseMsgHandler<RConversationKickMemberMsg> {

    @Autowired
    private WwDeviceService wwDeviceService;

    @Autowired
    private SendMsgProducer sendMsgProducer;

    @Autowired
    private ChatTalkSessionService chatTalkSessionService;

    @Autowired
    private ChatTalkSessionStaffRelService chatTalkSessionStaffRelService;

    @Override
    public Class<RConversationKickMemberMsg> msgClass() {
        return RConversationKickMemberMsg.class;
    }

    @Override
    public boolean checkMsg(RConversationKickMemberMsg baseMsg) {
        return super.checkMsg(baseMsg) || !ObjectUtils.isNotEmpty(baseMsg.getTalkSessionId()) || !ObjectUtils.isNotEmpty(baseMsg.getWxIds());
    }

    @Override
    public SResultMsg doHandle(RConversationKickMemberMsg content, ChannelHandlerContext ctx) {
        Long merId = content.getMerId();
        Long staffId = content.getStaffId();
        Long talkSessionId = content.getTalkSessionId();

        ChatTalkSession byId = chatTalkSessionService.getById(merId, talkSessionId);
        if (Objects.isNull(byId)) {
            throw new BusinessException("会话不存在: 退群失败");
        }
        //
        chatTalkSessionService.delChatSendDelayTask(merId, staffId, byId);
        chatTalkSessionService.delChatRecvDelayTask(merId, staffId, byId);
        //
        chatTalkSessionStaffRelService.delSessionRel(merId, staffId, byId.getDeviceUniqueId(), talkSessionId);

        MicaTriggerGroupExitMsg groupExitMsg = new MicaTriggerGroupExitMsg();
        groupExitMsg.setGroupId(byId.getConversationId());

        // 构建trigger对象
        MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(groupExitMsg, MicaTriggerType.TRIGGER_GROUP_EXIT, item -> {
            item.setReqId(IdUtil.getSnowflakeNextId());
            item.setDeviceId(byId.getDeviceId());
            item.setDeviceType(byId.getDeviceType());
        });
        sendMsgProducer.pushMsg(micaTriggerDto, MicaTopics.WW_TRIGGER_TOPIC);

        return SResultMsg.success(content, "退群成功");
    }

    @Override
    public ChatMsgType msgType() {
        return ChatMsgType.r_conversation_kick_member;
    }
}
